/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *   g.latarowski@gmail.com                                                *
 *   maciej.gajewski0@gmail.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


 

#include "gparamdescription.h"


static const char * PARAM_DESCRIPTION_TAG  = "paramDescription";
static const char * PARAMS_DESCRIPTION_TAG = "paramsDescription";

// param description properties names
static const char * NAME_TAG          = "name";
static const char * READABLE_NAME_TAG = "readableName";
static const char * TYPE_TAG          = "type";
static const char * DEFAULT_VALUE_TAG = "defaultValue";
static const char * MINIMUM_TAG       = "minimum";
static const char * MAXIMUM_TAG       = "maximum";
static const char * SINGLESTEP_TAG    = "singleStep";
static const char * DECIMALS_TAG      = "decimals";
static const char * ITEMS_TAG         = "items";





// ===========================================================================
//                             GParamDescription
// ===========================================================================



// -------------------- GParamDescription::GParamDescription() ---------------
GParamDescription::GParamDescription()
	: GXmlDocument()
{
		// clear nessesarry fields 
	_type     = QVariant::Invalid;
	_decimals = 0;
}
// ---------------------------------------------------------------------------


// -------------------- GParamDescription::toXml() ---------------------------
QDomElement GParamDescription::toXml( QDomDocument & doc )const
{
	QDomElement paramDescriptionElement;
	paramDescriptionElement = doc.createElement( PARAM_DESCRIPTION_TAG );

		// append name 
	QDomElement nameElement = doc.createElement( NAME_TAG );
	paramDescriptionElement.appendChild( nameElement );
	QDomText nameText = doc.createTextNode( _name );
	nameElement.appendChild( nameText );
		// append readableName 
	QDomElement readableNameElement = doc.createElement( READABLE_NAME_TAG );
	paramDescriptionElement.appendChild( readableNameElement );
	QDomText readableNameText = doc.createTextNode( _readableName );
	readableNameElement.appendChild( readableNameText );
		// append type
	QDomElement typeElement = doc.createElement( TYPE_TAG );
	paramDescriptionElement.appendChild( typeElement );
	QDomText typeText = doc.createTextNode( QVariant::typeToName( _type ) );
	typeElement.appendChild( typeText );
		// append defaultValue
	QDomElement defaultValueElement = doc.createElement( DEFAULT_VALUE_TAG );
	paramDescriptionElement.appendChild( defaultValueElement );
	defaultValueElement.appendChild( this->variantToXml( doc, _defaultValue ) );
		// append minimum
	QDomElement minimumElement = doc.createElement( MINIMUM_TAG );
	paramDescriptionElement.appendChild( minimumElement );
	minimumElement.appendChild( this->variantToXml( doc, _minimum ) );
		// append maximum
	QDomElement maximumElement = doc.createElement( MAXIMUM_TAG );
	paramDescriptionElement.appendChild( maximumElement );
	maximumElement.appendChild( this->variantToXml( doc, _maximum ) );
		// append singleStep
	QDomElement singleStepElement = doc.createElement( SINGLESTEP_TAG );
	paramDescriptionElement.appendChild( singleStepElement );
	singleStepElement.appendChild( this->variantToXml( doc, _singleStep ) );
		// append decimals
	QDomElement decimalsElement = doc.createElement( DECIMALS_TAG );
	paramDescriptionElement.appendChild( decimalsElement );
	QDomText decimalsText = doc.createTextNode( QString::number( _decimals ) );
	decimalsElement.appendChild( decimalsText );
	
		// append _items
	QDomElement itemsElement = doc.createElement( ITEMS_TAG );
	paramDescriptionElement.appendChild( itemsElement );
	itemsElement.appendChild( _items.toXml( doc ) );
	
	return paramDescriptionElement;
}
// ---------------------------------------------------------------------------


// --------------------- GParamDescription::fromXml() ------------------------
void GParamDescription::fromXml( const QDomElement & domElement )
{
	QDomElement descElement;
		// check if searched node isn't root 
	if( domElement.tagName() == PARAM_DESCRIPTION_TAG )
	{
		descElement = domElement;
	}
	else
	{
		descElement = domElement.firstChildElement( PARAM_DESCRIPTION_TAG );
	}
		// if there is no descElement then give up
	if( descElement.isNull() )
	{
		return;
	}

		// parse name
	QDomElement nameElement = descElement.firstChildElement( NAME_TAG );
	if( !nameElement.isNull() )
	{
		_name = nameElement.text();
	}
		// parse readableName
	QDomElement readableNameElement = descElement.firstChildElement( READABLE_NAME_TAG );
	if( !readableNameElement.isNull() )
	{
		_readableName = readableNameElement.text();
	}
		// parse type
	QDomElement typeElement = descElement.firstChildElement( TYPE_TAG );
	if( !typeElement.isNull() )
	{
		_type = QVariant::nameToType( qPrintable( typeElement.text() ) );
	}
	
		// parse defaultValue
	QDomElement defaultValueElement = descElement.firstChildElement( DEFAULT_VALUE_TAG );
	_defaultValue = this->variantFromXml( defaultValueElement );
		// parse minimim
	QDomElement minimimElement = descElement.firstChildElement( MINIMUM_TAG );
	_minimum = this->variantFromXml( minimimElement );
		// parse maximum
	QDomElement maximumElement = descElement.firstChildElement( MAXIMUM_TAG );
	_maximum = this->variantFromXml( maximumElement );
		// parse singleStep
	QDomElement singleStepElement = descElement.firstChildElement( SINGLESTEP_TAG );
	_singleStep = this->variantFromXml( singleStepElement );
		// parse decimals
	QDomElement decimalsElement = descElement.firstChildElement( DECIMALS_TAG );
	QVariant decimalsVariant( decimalsElement.text() );
	_decimals = decimalsVariant.toInt();

		// parse items
	QDomElement itemsElement = descElement.firstChildElement( ITEMS_TAG );
	_items.clear();
	_items.fromXml( itemsElement );
}
// ---------------------------------------------------------------------------





// ===========================================================================
//                           GParamsDescription
// ===========================================================================


// -------------------- GParamsDescription::toXml() --------------------------
QDomElement GParamsDescription::toXml( QDomDocument & doc )const
{
	QDomElement paramsDescriptionElement;
	paramsDescriptionElement = doc.createElement( PARAMS_DESCRIPTION_TAG );

		// add all descriptions
	QListIterator< GParamDescription > i(*this );
	while( i.hasNext() )
	{
		paramsDescriptionElement.appendChild( i.next().toXml( doc ) );
	}
	
	return paramsDescriptionElement;
}
// ---------------------------------------------------------------------------


// --------------------- GParamsDescription::fromXml() -----------------------
void GParamsDescription::fromXml( const QDomElement & domElement )
{
		// clear element in list
	this->clear();
	
	QDomElement paramsDescElement;
		// check if searched node isn't root 
	if( domElement.tagName() == PARAMS_DESCRIPTION_TAG )
	{
		paramsDescElement = domElement;
	}
	else
	{
		paramsDescElement = domElement.firstChildElement( PARAMS_DESCRIPTION_TAG );
	}
		// if there is no paramsDescElement then give up
	if( paramsDescElement.isNull() )
	{
		return;
	}
	
	// ----------------------------------------------------
	// parse params description

		// search for the first paramDescription
	QDomElement paramDescElement;
	paramDescElement = paramsDescElement.firstChildElement( PARAM_DESCRIPTION_TAG );
	while( !paramDescElement.isNull() )
	{
			// parse param description 
		GParamDescription paramDescription;
		paramDescription.fromXml( paramDescElement );
			// add extracted paramDescription to params list
		this->append( paramDescription );
			// search for next element
		paramDescElement = paramDescElement.nextSiblingElement( PARAM_DESCRIPTION_TAG );
	}
}
// ---------------------------------------------------------------------------


// ----------------- GParamsDescription::defaultParams() ---------------------
GParams GParamsDescription::defaultParams()const
{
		// ionitiate params map 
	GParams defaultParams;

			// add all descriptions
	QListIterator< GParamDescription > i(*this );
	while( i.hasNext() )
	{
		GParamDescription description = i.next();
		defaultParams.insert( description.name(), description.defaultValue() );
	}

	return defaultParams;
}
// ---------------------------------------------------------------------------





// eof
